package org.abc.fund.config;

import org.abc.fund.entity.user.Role;
import org.abc.fund.entity.user.User;
import org.abc.fund.repository.user.UserRepository;
import org.abc.fund.service.user.UserService;
import org.abc.fund.service.user.request.create_user.CreateUserRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class InitialUserConfig implements CommandLineRunner {
  private static final Logger logger = LoggerFactory.getLogger(InitialUserConfig.class);

  private final UserService userService;
  private final UserRepository userRepository;

  public InitialUserConfig(UserService userService, UserRepository userRepository) {
    this.userService = userService;
    this.userRepository = userRepository;
  }

  @Override
  public void run(String... args) throws Exception {
    createDefaultUsers();
  }

  private void createDefaultUsers() {
    // 创建一个管理员用户
    try {
      CreateUserRequest adminRequest = new CreateUserRequest();
      adminRequest.setUsername("admin");
      adminRequest.setPassword("admin123");
      adminRequest.setEmail("admin@example.com");
      adminRequest.setRealName("管理员小二");
      adminRequest.setGender(User.Gender.MALE);
      adminRequest.setAge(50);
      adminRequest.setRoles(
          Set.of(Role.ADMIN, Role.RESEARCHER, Role.ADVISOR, Role.TRADER)); // 管理员可以有多个角色
      userService.createUser(adminRequest);
    } catch (IllegalArgumentException e) {
    }

    // 创建一个投研人员用户
    try {
      CreateUserRequest researcherRequest = new CreateUserRequest();
      researcherRequest.setUsername("researcher01");
      researcherRequest.setPassword("password123");
      researcherRequest.setEmail("researcher01@example.com");
      researcherRequest.setRealName("研究员张三");
      researcherRequest.setGender(User.Gender.FEMALE);
      researcherRequest.setAge(30);
      researcherRequest.setRoles(Set.of(Role.RESEARCHER));
      userService.createUser(researcherRequest);
    } catch (IllegalArgumentException e) {
    }

    // 创建一个投顾人员用户
    try {
      CreateUserRequest advisorRequest = new CreateUserRequest();
      advisorRequest.setUsername("advisor01");
      advisorRequest.setPassword("password123");
      advisorRequest.setEmail("advisor01@example.com");
      advisorRequest.setRealName("投顾李四");
      advisorRequest.setGender(User.Gender.MALE);
      advisorRequest.setAge(25);
      advisorRequest.setRoles(Set.of(Role.ADVISOR));
      userService.createUser(advisorRequest);
    } catch (IllegalArgumentException e) {
    }

    // 创建一个交易人员用户
    try {
      CreateUserRequest traderRequest = new CreateUserRequest();
      traderRequest.setUsername("trader01");
      traderRequest.setPassword("password123");
      traderRequest.setEmail("trader01@example.com");
      traderRequest.setRealName("交易员王五");
      traderRequest.setGender(User.Gender.FEMALE);
      traderRequest.setAge(20);
      traderRequest.setRoles(Set.of(Role.TRADER));
      userService.createUser(traderRequest);
    } catch (IllegalArgumentException e) {
    }

    // 创建一个客户
    try {
      CreateUserRequest clientRequest = new CreateUserRequest();
      clientRequest.setUsername("client01");
      clientRequest.setPassword("password123");
      clientRequest.setEmail("client01@example.com");
      clientRequest.setRealName("客户小六");
      clientRequest.setGender(User.Gender.MALE);
      clientRequest.setAge(18);
      clientRequest.setRoles(Set.of(Role.CLIENT));
      userService.createUser(clientRequest);
    } catch (IllegalArgumentException e) {
    }

    logger.info("默认用户已创建。");
  }
}
